﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCDataCenter;
using BCDataCenter.BOL;

public partial class WebUserControl_uscChangePassSales : System.Web.UI.UserControl
{
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Salesp_key_Logon"] == null)
            {
                Response.Redirect(Globals.ApplicationPath);
            }
           
        }
        lblMaTVV.Text = Session["Salesp_key_Logon"].ToString();
        getTenTVV(lblMaTVV.Text);
       
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        if (checkUser(lblMaTVV.Text, txtOldPass.Text))
        {
            BWChangePassTVV obj = new BWChangePassTVV();
            if (obj.ChangePassword(ref error, lblMaTVV.Text, txtNewPass.Text))
            {
                uscMsgBox1.AddMessage("Thay đổi mật mã thành công.", uscMsgBox.enmMessageType.Success);
            }
            else
            {
                uscMsgBox1.AddMessage("Thay đổi mật mã thất bại." + "\n" + error, uscMsgBox.enmMessageType.Error);
            }
        }
        else
        {
            uscMsgBox1.AddMessage("Mật mã cũ không chính xác.", uscMsgBox.enmMessageType.Error);
        }
    }
    private void getTenTVV(string MaTVV)
    {
        BWChangePassTVV obj = new BWChangePassTVV();
        DataSet dsUserLogon = obj.dsGetTenTVV(ref error, MaTVV);
        if (dsUserLogon.Tables.Count > 0)
        {
            if (dsUserLogon.Tables[0].Rows.Count > 0)
            {
                lblTenTVV.Text = dsUserLogon.Tables[0].Rows[0]["salesp_name"].ToString();
            }
        }        
    }

    private bool checkUser(string UserName, string Pass)
    {
        
        BWUser obj = new BWUser();
        DataSet dsUserLogon = obj.dsGetUserViewReportLogon(ref error, UserName, Pass);
        if (dsUserLogon.Tables.Count < 1)
        {
            return false;
        }
        else if (dsUserLogon.Tables[0].Rows.Count < 1)
        {
            return false;
        }
        else
            return true;
    }
}
